# Maintainer: Martell Malone < martell malone at g mail dot com >

_realname=go
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.7
pkgrel=1
pkgdesc="Go Lang (mingw-w64)"
arch=('any')
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
url="https://www.golang.org/"
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip')
source=("https://golang.org/dl/go${pkgver}.src.tar.gz"
        0010-go1.7-proper-go-binary-path.patch)
sha256sums=('72680c16ba0891fcf2ccf46d0f809e4ecf47bbf889f5d884ccb54c5e9a17e1c0'
            'feccfe481221f063acef00f7d9daf33ce0f369b2a065513067d8308d56258cc5')

prepare() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  mv "${srcdir}"/${_realname} "${srcdir}"/${_realname}-${pkgver}

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0010-go1.7-proper-go-binary-path.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}/src
  export GOROOT_BOOTSTRAP=/`echo ${MSYSTEM} | tr '[:upper:]' '[:lower:]'`/lib/go
  export GOROOT="${srcdir}/${_realname}-${pkgver}"
  export GOBIN="${GOROOT}/bin"
  export GOPATH="${srcdir}/"
  export GOROOT_FINAL=${MINGW_PREFIX}/lib/go
  export GO_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  export CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  ./make.bat
}

package() {
  cd "${_realname}-${pkgver}"

  export GOROOT="${srcdir}/${_realname}-${pkgver}"
  export GOBIN="${GOROOT}/bin"

# install -Dm755 "${srcdir}/godoc" "${pkgdir}${MINGW_PREFIX}/bin/godoc"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/go"
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/go/LICENSE"

  mkdir -p \
    "${pkgdir}${MINGW_PREFIX}/"{share/go,lib/go,lib/go/src,lib/go/site/src}

  cp -r doc misc -t                  "${pkgdir}${MINGW_PREFIX}/share/go"
  cp -a bin                          "${pkgdir}${MINGW_PREFIX}"
  cp -a pkg                          "${pkgdir}${MINGW_PREFIX}/lib/go"
  cp -a "$GOROOT/src"                "${pkgdir}${MINGW_PREFIX}/lib/go/"
  cp -a "$GOROOT/src/cmd"            "${pkgdir}${MINGW_PREFIX}/lib/go/src/cmd"
  cp -a "$GOROOT/lib"                "${pkgdir}${MINGW_PREFIX}/lib/go/"

  install -Dm644 src/Make.* "${pkgdir}${MINGW_PREFIX}/lib/go/src"

  # Remove object files from target src dir
  find "${pkgdir}${MINGW_PREFIX}/lib/go/src/" -type f -name '*.[ao]' -delete

  # Fix for FS#32813
  find "${pkgdir}" -type f -name sql.go -exec chmod -x {} \;

  # Remove all executable source files
  find "${pkgdir}${MINGW_PREFIX}/lib/go/src" -type f -executable -delete

  # This is to make go get code.google.com/p/go-tour/gotour and
  # then running the gotour executable work out of the box.
  # ln -sf ${MINGW_PREFIX}/bin "${pkgdir}${MINGW_PREFIX}/lib/go/bin"

  # For FS#42660 / FS#42661 / gox
  install -Dm755 src/make.bash "${pkgdir}${MINGW_PREFIX}/lib/go/src/make.bash"
  install -Dm755 src/run.bash "${pkgdir}${MINGW_PREFIX}/lib/go/src/run.bash"
  cp -r misc/ "${pkgdir}${MINGW_PREFIX}/lib/go/"

  # For godoc
  install -Dm644 favicon.ico "${pkgdir}${MINGW_PREFIX}/lib/go/favicon.ico"

  rm -f "${pkgdir}${MINGW_PREFIX}/share/go/doc/articles/wiki/get.bin"

  install -Dm644 VERSION "${pkgdir}${MINGW_PREFIX}/lib/go/VERSION"

  find "${pkgdir}${MINGW_PREFIX}/"{lib/go/pkg,bin} -type f -exec touch '{}' +
 
  # install profile script 
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d
  echo "export GOROOT=${MINGW_PREFIX}/lib/go" > "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.sh
  cp "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.{sh,zsh}
}
